//
//  NearbyFriendCell.m
//  PigMarket
//
//  Created by mac on 2019/6/11.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "NearbyFriendCell.h"
#import "NearbyPhotos.h"

@interface NearbyFriendCell ()
/**
 头像
 */
@property (nonatomic , strong) UIImageView *headImg;
/**
 name
 */
@property (nonatomic , strong) UILabel *nameLab;
/**
 性别
 */
@property (nonatomic , strong) UIImageView *sexImg;
/**
 年龄
 */
@property (nonatomic , strong) UILabel *ageLab;
/**
 个性签名
 */
@property (nonatomic , strong) UILabel *remarkLab;
/**
 距离
 */
@property (nonatomic , strong) UILabel *distanceLab;
/**
 照片
 */
@property (nonatomic , strong) NearbyPhotos *photoView;


@property (nonatomic , strong) UIButton *addFriendBtn;

@property (nonatomic , assign) AddFriendOrChatType addFriendOrChatType;

@end


@implementation NearbyFriendCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifierID = @"identifierID";
    NearbyFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (cell == nil) {
        cell = [[NearbyFriendCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
#pragma mark - init method
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self.contentView addSubview:self.headImg];
    [self.contentView addSubview:self.distanceLab];
    [self.contentView addSubview:self.nameLab];
    [self.contentView addSubview:self.sexImg];
    [self.contentView addSubview:self.ageLab];
    [self.contentView addSubview:self.remarkLab];
    [self.contentView addSubview:self.photoView];
 //   [self.contentView addSubview:self.addFrindAndGotoShopBgView];
//    [self.addFrindAndGotoShopBgView addSubview:self.sLine];
    [self.contentView addSubview:self.addFriendBtn];
//    [self.addFrindAndGotoShopBgView addSubview:self.gotoShopBtn];
//    [self.addFrindAndGotoShopBgView addSubview:self.shopNumIcon];
}

- (void)initLayout {
    // 布局子控件
    [self.headImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(15);
        make.size.mas_equalTo(CGSizeMake(42, 42));
    }];
    
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headImg.mas_right).offset(12);
        make.top.mas_equalTo(18);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(KScreenWidth - 200);
    }];

    
    [self.sexImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLab.mas_right).offset(5);
        make.centerY.mas_equalTo(self.nameLab.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(14, 14));
    }];
    
    
    [self.ageLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headImg.mas_right).offset(12);
        make.top.mas_equalTo(self.nameLab.mas_bottom).offset(2);
        make.height.mas_equalTo(17);
        make.width.mas_equalTo(55);
    }];
    
    [self.distanceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(self.ageLab.mas_top);
        make.height.mas_equalTo(20);
        make.left.mas_equalTo(self.ageLab.mas_right).offset(2);
    }];

    [self.remarkLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(self.headImg.mas_bottom).offset(12);
        make.right.mas_equalTo(-15);
    }];
    [self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(self.remarkLab.mas_bottom).offset(10);
        make.height.mas_equalTo(0);
        make.bottom.mas_equalTo(-15);

    }];

    [self.addFriendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(27);
        make.width.mas_equalTo(58);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(23);
    }];
}


- (void)addFriendBtnClick {
    if (self.addOrChatBlock) {
        self.addOrChatBlock(self.addFriendOrChatType);
    }
}


#pragma mark - getter and setter
- (void)setModel:(NearbyFriends *)model {
    _model = model;
    [self.headImg sd_setImageWithURL:[NSURL URLWithString:model.headImg] placeholderImage:[UIImage imageNamed:@"placeholder_header"]];
    self.remarkLab.text = model.personality;
    self.distanceLab.text = [NSString stringWithFormat:@"%.2fkm",model.ranges];
    self.ageLab.text = [NSString stringWithFormat:@"年龄: %@",model.age];

    if (model.imgs.count == 1) {
        self.photoView.hidden = NO;
        [self.photoView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(128 * [JTool getScreenWidthScale]);
        }];
        [self.photoView configPhotosWithImgsArr:model.imgs];
    } else if (model.imgs.count == 2 || model.imgs.count == 3) {
        self.photoView.hidden = NO;
        [self.photoView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(109 * [JTool getScreenWidthScale]);
        }];
        [self.photoView configPhotosWithImgsArr:model.imgs];
    } else {
        self.photoView.hidden = YES;
        [self.photoView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }

    switch ([model.friendIf intValue]) {
        case IsFrinedStatusYes:
        {
            [self.addFriendBtn setTitle:@"聊一聊" forState:UIControlStateNormal];
            self.addFriendOrChatType = AddFriendOrChatTypeChat;
            [self.addFriendBtn setTitleColor:KWhiteColor forState:UIControlStateNormal];
            self.addFriendBtn.backgroundColor = mMainColor;
        }
        break;
        case IsFrinedStatusNo:
        {
            [self.addFriendBtn setTitle:@"加好友" forState:UIControlStateNormal];
            self.addFriendOrChatType = AddFriendOrChatTypeAdd;
            [self.addFriendBtn setTitleColor:mMainColor forState:UIControlStateNormal];
            self.addFriendBtn.backgroundColor = KWhiteColor;
        }
            break;
        default:
            break;
    }
//    switch ([model.sex intValue]) {
//        case SexTypeMan:
//            self.sexImg.image = [UIImage imageNamed:@"user_man"];
//            break;
//        case SexTypeWoman:
//            self.sexImg.image = [UIImage imageNamed:@"user_woman"];
//            break;
//        default:
//            break;
//    }
    
    NSString *image;
    switch ([model.sex intValue]) {
        case SexTypeMan:
            image = @"user_man";
            break;
        case SexTypeWoman:
            image = @"user_woman";
            break;
        default:
            break;
    }

    NSString *text = [NSString stringWithFormat:@"%@  ", model.name];
    NSMutableAttributedString *aim = [text createAttributingByIcon:nil downIndexImage:image textColor:UIColorFromRGB(0x343434) font:mMediumFont(14)];
    _nameLab.attributedText = aim;

}

- (UIImageView *)headImg {
    if (!_headImg) {
        _headImg = [[UIImageView alloc] init];
        _headImg.layer.cornerRadius = 21;
        _headImg.layer.masksToBounds = YES;
    }
    return _headImg;
}

- (UILabel *)nameLab {
    if (!_nameLab) {
        _nameLab = [[UILabel alloc] init];
        _nameLab.textColor = UIColorFromRGB(0x343434);
        _nameLab.font = mMediumFont(14);
    }
    return _nameLab;
}

- (UIImageView *)sexImg {
    if (!_sexImg) {
        _sexImg = [[UIImageView alloc] init];
        _sexImg.layer.cornerRadius = 3;
        _sexImg.layer.masksToBounds = YES;
    }
    return _sexImg;
}
- (UILabel *)distanceLab {
    if (!_distanceLab) {
        _distanceLab = [[UILabel alloc] init];
        _distanceLab.textColor = [UIColor colorWithHexString:@"#B2B2B2"];
        _distanceLab.font = [UIFont smallFont];
        _distanceLab.textAlignment = 0;
    }
    return _distanceLab;
}
- (UILabel *)ageLab {
    if (!_ageLab) {
        _ageLab = [[UILabel alloc] init];
        _ageLab.textColor = [UIColor colorWithHexString:@"#B2B2B2"];
        _ageLab.font = [UIFont smallFont];
    }
    return _ageLab;
}
- (UILabel *)remarkLab {
    if (!_remarkLab) {
        _remarkLab = [[UILabel alloc] init];
        _remarkLab.textColor = [UIColor colorWithHexString:@"#7F7F7F"];
        _remarkLab.font = [UIFont middleFont];
        _remarkLab.numberOfLines = 0;
    }
    return _remarkLab;
}

- (UIButton *)addFriendBtn {
    if (!_addFriendBtn) {
        _addFriendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addFriendBtn setTitleColor:mMainColor forState:UIControlStateNormal];
        _addFriendBtn.titleLabel.font = mFont(12);
        [_addFriendBtn addTarget:self action:@selector(addFriendBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _addFriendBtn.layer.borderColor = mMainColor.CGColor;
        _addFriendBtn.layer.borderWidth = 1;
        _addFriendBtn.backgroundColor = KWhiteColor;
        [self addShadowAndCircleCorner:_addFriendBtn.layer corner:5];
    }
    return _addFriendBtn;
}

- (NearbyPhotos *)photoView {
    if (!_photoView) {
        _photoView = [[NearbyPhotos alloc] init];
    }
    return _photoView;
}

@end
